package br.com.hsp.system.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;


import br.com.hsp.system.repository.UserRepository;
import br.com.hsp.users.Usuario;

@ManagedBean
public class UserBean {

	private Usuario user = new Usuario();

	public void add() {
		EntityManager manager = this.getManager();
		UserRepository userRepository = new UserRepository(manager);
		
			
		if (this.user.getId() == null) {
			userRepository.add(this.user);
		} else {
			userRepository.atualiza(this.user);
		}
		this.user = new Usuario();

	}




	

	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}






	public Usuario getUser() {
		return user;
	}






	public void setUser(Usuario user) {
		this.user = user;
	}

	
	
	
	

}
